package com.ljd.sps;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

public class Util {

    private static final String TAG = "ljd";

    private static final String[] S4_MODEL_ARRAY = new String[] { "GT-i9500", "GT-i9500X", "GT-i9502", "GT-i9505",
            "GT-i9505T", "SHV-E300S", "SHV-E300K", "SHV-E300L", "SGH-N045", "SCH-i545" };

    public static boolean isS3() {
        return android.os.Build.BRAND.equalsIgnoreCase("samsung")
                && android.os.Build.MODEL.equalsIgnoreCase("GT-I9300");
    }

    public static boolean isS4() {
        return android.os.Build.BRAND.equalsIgnoreCase("samsung") && isS4Model();
    }

    private static boolean isS4Model() {
        for (int i = 0; i < S4_MODEL_ARRAY.length; i++) {
            if (android.os.Build.MODEL.equalsIgnoreCase(S4_MODEL_ARRAY[i])) {
                return true;
            }
        }

        return false;
    }

    //ImageUtil

    public static final String SDCARD_STORAGE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();

    public static Bitmap loadBitmapWithSizeLimitation(Context context, int maximum_size, Uri uri) {
        Bitmap image = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();

        try {
            InputStream is = context.getContentResolver().openInputStream(uri);
            opts.inJustDecodeBounds = true;
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            BitmapFactory.decodeStream(is, null, opts);
            int size = opts.outWidth * opts.outHeight;
            int downScale = 1;
            while (size > maximum_size) {
                size = (size >> 2);
                downScale = (downScale << 1);
            }
            try {
                is.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }

            opts.inJustDecodeBounds = false;
            opts.inSampleSize = downScale;
            is = context.getContentResolver().openInputStream(uri);
            image = BitmapFactory.decodeStream(is, null, opts);
            try {
                is.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        } catch (java.io.FileNotFoundException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            // Do nothing - the photo will appear to be missing
        } catch (Exception e) {
            e.printStackTrace();
        }
        //      saveBitmapWithGivenSize(context,image);
        return image;
    }

    public static Bitmap loadBitmapWithSizeLimitation(Context context, int maximum_size, String filename, String path) {
        File file = new File(path + File.separator + filename);

        Uri uri = Uri.fromFile(file);

        return loadBitmapWithSizeLimitation(context, maximum_size, uri);
    }

    public static int getImageRotateDegree(String filepath) {
        ExifInterface exif = null;
        int orientation = -1;
        try {
            exif = new ExifInterface(filepath);
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int degree = 0;
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
            break;
        }
        return degree + 90;
    }

    public static byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static RectF getLastChooseArea(SharedPreferences sp, int type) {
        RectF rect = null;

        final float left = sp.getFloat("Left" + type, -1);
        final float top = sp.getFloat("Top" + type, -1);
        final float right = sp.getFloat("Right" + type, -1);
        final float bottom = sp.getFloat("Bottom" + type, -1);

        if (SmallphoneScannerActivity.LOG) {
            Log.d(TAG, "getArea:" + type + "=" + left + "|" + top + "|" + right + "|" + bottom);
        }
        if (left > -1 && top > -1 && right > -1 && bottom > -1) {
            rect = new RectF(left, top, right, bottom);
        }
        return rect;
    }

    public static void setLastChooseArea(SharedPreferences sp, int type, RectF rect) {
        if (rect == null) {
            if (SmallphoneScannerActivity.LOG) {
                Log.d(TAG, "setArea:" + type + "=null");
            }
            return;
        }

        Editor edit = sp.edit();

        edit.putFloat("Left" + type, rect.left);
        edit.putFloat("Top" + type, rect.top);
        edit.putFloat("Right" + type, rect.right);
        edit.putFloat("Bottom" + type, rect.bottom);

        edit.commit();

        if (SmallphoneScannerActivity.LOG) {
            Log.d(TAG, "setArea:" + type + "=" + rect.left + "|" + rect.top + "|" + rect.right + "|" + rect.bottom);
        }
    }
}
